<div class="widget">
	<div class="widget-header bg-blue">
		<i class="widget-icon icon-notebook"></i> 
		<span class="widget-caption"> 新增班级 </span>
		<div class="widget-buttons">
			<a href="javascript:void(0);" onclick="handlerCancel(this);" title="关闭"
				 data-toggle="dispose" data-refresh-table="#table-classes"> <i class="fa fa-times"></i> </a> 
		</div>
	</div>
	<div class="widget-body">
		<!-- TODO 基本信息 -->
		<div class="widget radius-bordered mgbt-xs-10">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 基本信息 </span>
			</div>
			<div class="widget-body editor-body-sm">
				<form class="form-horizontal form-editor form-editor-classes">
					<div class="row">
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label" for="lesson">课程选择</label>
								<div class="col-sm-8">
									<select name="relations[0].targetid" id="lesson" class="chosen-select"
										data-entity="com.app.miwawa.api.eam.entity.Lesson">
									</select>
									<input name="relations[0].target" type="hidden" value="lesson">
									<input name="relations[0].status" type="hidden" value="0">
									<input name="relations[0].invalid" type="hidden" value="F">
								</div>
							</div>
						</div>
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label" for="lessonTime">上课时间</label>
								<div class="col-sm-8">
									<div class="input-group">
										<span class="input-group-btn">
											<select name="times[0].week" id="lessonWeek" class="chosen-select">
												<option value="周一">周一</option>
												<option value="周二">周二</option>
												<option value="周三">周三</option>
												<option value="周四">周四</option>
												<option value="周五">周五</option>
												<option value="周六">周六</option>
												<option value="周日">周日</option>
											</select>
										</span>
										<input name="times[0].time" id="lessonTime" type="text" class="form-control time-selector"> 
									</div>
									<input name="times[0].type" type="hidden" value="早教">
									<input name="times[0].mode" type="hidden" value="from">
									<input name="times[0].status" type="hidden" value="0">
									<input name="times[0].invalid" type="hidden" value="F">
								</div>
							</div>
						</div>
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label" for="room">教室选择</label>
								<div class="col-sm-8">
									<select name="relations[1].targetid" id="room" class="chosen-select"
										data-entity="com.app.miwawa.api.eam.entity.Room">
									</select>
									<input name="relations[1].target" type="hidden" value="room">
									<input name="relations[1].status" type="hidden" value="0">
									<input name="relations[1].invalid" type="hidden" value="F">
								</div>
							</div>
						</div>
					</div>
					<div class="row">
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label" for="createDate">开班时间</label>
								<div class="col-sm-8">
									<div class="input-group date date-selector">
										<input name="createDate" id="createDate" type="text" class="form-control"> 
										<span class="input-group-addon">
											<span class="fa fa-calendar"></span>
										</span>
									</div>
								</div>
							</div>
						</div>
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label required" for="toplimit">学员上限</label>
								<div class="col-sm-8">
									<div class="input-group">
										<input name="toplimit" id="toplimit" type="text" class="form-control" required="required"/>
										<span class="input-group-addon">人</span>
									</div>
								</div>
							</div>
						</div>
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label required" for="trylimit">试听上限</label>
								<div class="col-sm-8">
									<div class="input-group">
										<input name="trylimit" id="trylimit" type="text" class="form-control" required="required"/>
										<span class="input-group-addon">人</span>
									</div>
								</div>
							</div>
						</div>
					</div>
					<div class="row">
						<div class="col-sm-8">
							<div class="form-group">
								<label class="col-sm-2 control-label required" for="name">班级名称</label>
								<div class="col-sm-10">
									<div class="input-group">
										<input name="type" type="hidden">
										<input name="name" id="name" type="text" class="form-control" readonly="readonly" required="required"/>
										<span class="input-group-btn">
											<button type="button" class="btn btn-addon" data-toggle="tooltip" data-placement="top" title="生成名称" onclick="createName()">
												<i class="fa fa-refresh"></i>
											</button>
										</span>
									</div>
								</div>
							</div>
						</div>
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label required" for="usecount">耗课数</label>
								<div class="col-sm-8">
									<input name="usecount" id="usecount" type="text" class="form-control" value="1" required="required"/>
								</div>
							</div>
						</div>
					</div>
					
					<div class="widget-footer editor-footer-sm">
						<button type="button" class="btn btn-danger btn-sm" data-toggle="tooltip" data-placement="top" title="取消" onclick="handlerCancel(this, 'editor-wrapper-open');" data-refresh-table="#table-classes">
							<i class="glyphicon glyphicon-remove"></i>
						</button>
						<button type="button" class="btn btn-primary btn-sm" data-toggle="tooltip" data-placement="top" title="保存" onclick="handlerSubmit(this, '#editor-form-classes', '.form-editor-classes', 'inspect');" data-submit-callback="handlerCallback">
							<i class="glyphicon glyphicon-ok"></i>
						</button>
					</div>
				</form>
			</div>
		</div>
		
		<!-- TODO 老师信息 -->
		<div class="widget radius-bordered mgbt-xs-10 hidden" id="editor-panel-teacher">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 本班老师 </span>
				<div class="widget-buttons buttons-bordered">
					<button class="btn btn-default btn-sm" data-toggle="modal" data-target="#common-modal" data-url="content/eam/classes/dialog/teacher.html" data-onshow="onTeacherShow">
						<i class="fa fa-plus"></i>
					</button>
				</div>
			</div>
			<div class="widget-body editor-body-sm editor-body-teacher">
				<form class="table-editor" id="tables-form-teacher" data-validate="false">
					<table data-ajax="handlerSearch" 
								 data-method="post" 
								 data-id-field="id" 
								 data-unique-id="teacherid" 
								 data-pagination="true" 
								 data-click-to-select="true" 
								 data-side-pagination="server" 
								 data-mobile-responsive="true" 
								 data-form-source="#tables-form-teacher" 
								 data-form-target="#editor-form-teacher" 
								 data-form-search="#search-form-teacher" id="table-teacher" class="bootstrap-table">
						<thead>
							<tr>
								<th data-checkbox="true"></th>
								<th data-field="id" data-visible="false"></th>
								<th data-field="name" 		data-align="center">姓名</th>
								<th data-field="sex" 			data-align="center">性别</th>
								<th data-field="mobile" 	data-align="center">手机</th>
								<th data-field="method" 	data-align="center">收费方式</th>
								<th data-field="amount" 	data-align="center">费用</th>
								<th data-field="operator" data-align="center" data-width="100" data-formatter="renderOperator" data-table-target="teacher">操作</th>
							</tr>
						</thead>
					</table>
				</form>
			</div>
		</div>
		
	</div>
</div>

<!-- 提交表单 ========================================================= -->
<form id="editor-form-classes" action="/miwawa/{dbname}/classes/save" method="post" 
			name="com.app.miwawa.api.eam.entity.Classes"
			data-show-target="#editor-panel-teacher"
			data-custom-action="true">
			<input type="hidden" name="status" value="0">
			<input type="hidden" name="invalid" value="F">
</form>
<form id="editor-form-teacher" action="/miwawa/{dbname}/common/update" method="post" 
			name="com.app.miwawa.api.eam.entity.ClassesCharge">
			<input type="hidden" name="bean['teacherid']" id="teacherid">
			<input type="hidden" name="bean['classesid']" id="classesid">
			<input type="hidden" name="bean['status']" 		value="0">
			<input type="hidden" name="bean['invalid']" 	value="F">
</form>
<!-- 查询 表单 ========================================================= -->
<form id="search-form-teacher" action="/miwawa/{dbname}/common/search.json" method="post" 
			name="com.app.miwawa.api.eam.entity.view.ClassesTeacher">
			<input type="hidden" name="classesid" value="0" data-search-group="and" data-search-operation="EQ">
</form>

<script type="text/javascript">
<!--
	var $moment;

	jQuery(function() {
		require(['jquery.plugin.form','moment'],function(form,moment){
			form.datepicker(".date-selector");
			form.datepicker(".time-selector",{format:"HH:mm"});
			form.tooltip("[data-toggle='tooltip']");
			
			form.chosen("#lessonWeek");
			form.chosen("#lesson").select("#lesson",{ "querys":{"invalid_EQ":"F"} });
			form.chosen("#room").select("#room",{ "querys":{"invalid_EQ":"F","type_EQ":"早教"} });
			$moment = moment;
		});
		
		// 加载老师列表
		jQuery("#table-teacher").bootstrapTable();
	});
	
	var classesname;
 	var roomname;
	function createName(){
		var week = jQuery("#lessonWeek").val();
		var time = jQuery("#lessonTime").val();
		var lesson = jQuery("#lesson").val();
		lesson = jQuery("#lesson > option[value='"+lesson+"']").text();
		jQuery("#name").val(week+time+lesson);
		if((/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/).test(jQuery("#usecount").val()) == false){
			jQuery.message.warning("耗课数只能为正整数或正小数!");
		}
		if(check(week)){
			jQuery.message.alert(week+" "+time+" 已有班级： "+classesname+" 在 "+roomname+" 上课，请修改上课时间或选择其他教室");
		}
	}
	
	function inspect(){
		var week = jQuery("#lessonWeek").val();
		var time = jQuery("#lessonTime").val();
		if(jQuery("#name").val()==""){
			jQuery.message.alert("请点击班级名称右侧“生成名称”按钮，生成班级名称");
			return false;
		}else if((/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/).test(jQuery("#usecount").val()) == false){
			jQuery.message.warning("耗课数只能为正整数或正小数!");
			return false;
		}else if(check(week)){
			jQuery.message.alert(week+" "+time+" 已有班级： "+classesname+" 在 "+roomname+" 上课，请修改上课时间或选择其他教室");
			return false;
		}else{
			return true;
		}
	}
	
	//检查该时间段该教室是否已有班级上课
	function check(week){
		var stamp = false;
		jQuery.ajax({
			url:"/miwawa/{dbname}/common/search.json".replace("{dbname}",dbname),
			type:"POST",
			dataType:"json",
			async:false,
			data:{
	       		"entityClass" : "com.app.miwawa.api.eam.entity.view.ClassesView",
	          "and[0][operator]": "EQ",
	          "and[0][fieldName]": "type",					
	          "and[0][fieldValue]": "早教",
	          "and[1][operator]": "EQ",
	          "and[1][fieldName]": "week",					
	          "and[1]][fieldValue]": week,	
	          "and[2][operator]": "EQ",
	          "and[2][fieldName]": "roomid",					
	          "and[2][fieldValue]": jQuery("#room").val(),
	          "and[3][operator]": "EQ",
	          "and[3][fieldName]": "invalid",					
	          "and[3][fieldValue]": "F"
	  	},
	  	success:function(data){
	  		var list = data.rows;
	  		for(var i=0; i<list.length; i++){
	  			var time = $moment("2016-09-14 "+list[i].time);
		  			if($moment("2016-09-14 "+jQuery("#lessonTime").val()+":00").add(45,"m").isAfter(time) && time.isAfter($moment("2016-09-14 "+jQuery("#lessonTime").val()+":00").add(-45,"m"))){
		  				classesname = list[i].name;
		  				roomname = list[i].room;
		  				stamp = true;
		  				break;
		  			} 
	  		}		
	  	}
		});
		return stamp;
	}
	
	function setFormData(dest, orig){
		jQuery("#teacherid", orig).val(teacher.teacherid);
		jQuery("#classesid", orig).val(teacher.classesid);
		return true;
	}
	
	var classes;
	function handlerCallback(){
		var classesid = jQuery("#editor-form-classes").data("com.app.miwawa.api.eam.entity.Classes");
		classes = {"id":classesid};
		jQuery(":hidden[name='classesid']","#search-form-teacher").val(classesid);
	}
	
	// 更新负责老师收费项
	var teacher;
	function handlerUpdate(trigger, index){
		var _opts = jQuery(trigger).data();
				teacher = _opts.rowdata;
		
		jQuery("#table-teacher").bootstrapTable("updateRow",{
			index: index,
			row: createEditRow(_opts.rowdata, _opts.tableSource)
		});
		
		function createEditRow(row, target){
			jQuery(target).append("<input type='hidden' name='id' value='"+row.chargeid+"'>");
			
			return {
				id:   		"edit",
	      name: 		row.name,
	      sex: 			row.sex,
	      mobile: 	row.mobile,
	      method: 	createInput('method',	'select', 'ext-text', row.method),
	      amount: 	createInput('amount',	'number', 'ext-text', row.amount),
	      operator: createButton(target)
			};
		}
		
		// 创建输入框
		function createInput(name, type, ext, value){
			switch(type){
				case 'number': 
					return '<input name="'+name+'" id="'+name+'" type="'+type+'" class="form-control '+ext+'" value="'+(value ? value : '10.00')+'">';
				case 'select': 
					return '<select name="'+name+'" id="'+name+'" class="form-control">'
								+	'<option '+(value == '按课时' ? 'selected="selected"' : '')+' value="按课时">按课时</option>'
								+ '<option '+(value == '按人头' ? 'selected="selected"' : '')+' value="按人头">按人头</option>'
								+'</select>';
			}
		}
		
		// 创建按钮
		function createButton(target){
			return ""
						+"<span class='table-editor-tools'>"
						+"	<button type='button' class='btn btn-danger  btn-xs' onclick='handlerCancelRow(\""+target+"\", true);' data-toggle='tooltip' data-placement='top' title='取消'><i class='icon ion-close-circled'></i></button>"
						+"	<button type='button' class='btn btn-success btn-xs' onclick='handlerSubmitRow(\""+target+"\",\"setFormData\");' data-toggle='tooltip' data-placement='top' title='确认'><i class='icon ion-checkmark-circled'></i></button>"
						+"</span>";
		}
		
		jQuery("[data-toggle='tooltip']").tooltip({html:true, container:"html", trigger:'hover'});
	}
	
//-->
</script>